require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['zepto','common/mcommon'],function($,com){
		
		$.fn.prevAll = function(selector){
		 var prevEls = [];
		 var el = this[0];
		 if(!el) return $([]);
		 while (el.previousElementSibling) {
		        var prev = el.previousElementSibling;
		        if (selector) {
		          if($(prev).is(selector)) prevEls.push(prev);
		        }
		        else prevEls.push(prev);
		        el = prev;
		      }
		      return $(prevEls);
		};

        com.init();
		var pageLogic = new PageLogic($,com);
		pageLogic.init();

	});
});
var PageLogic = function($,com){
	return{
		init:function(){
			var _this = this;
			_this.initData();
		},
		initData:function(){
			var _this = this;
			com.ajax({
				url:"/activity_init.json",
				success:function(res){
					_this.initYear(res.data.years);
				}
			})
		},
		initYear:function(years){
			var _this = this;
			var html = "";
			var first = "";
			var cls = "";
			$(years).each(function(idx){
				if(idx == 0){
					first = this;
					cls = "toggle";
				}else{
					cls = "";
				}
				html += '<div class="year-info" tg="' + this + '"> ';
				html += '  <div class="title-item ' + cls + ' " tg="'  + this  + '"> ';
				html += '	<span class="title">' + this + '<i></i></span> ';
				html += '  </div> ';
				html += '  <div class="year-items"> ';
				html += '  </div> ';
				html += '</div> ';	
			});
			$(".report-list").html(html);
			_this.yearInfo(first);
			$(".report-list").find(".title-item").on("click",function(){
				if($(this).hasClass("toggle")){
					$(this).removeClass("toggle");
				}else{
					//$(".report-list").find(".title-item").removeClass("toggle");
					$(this).addClass("toggle");
					var yearItems = $(this).parent().find(".year-items").html();
					var current = $(this).attr("tg");
					if(!$.trim(yearItems)){
						_this.yearInfo(current);
					}
				}
			});
		},
		yearInfo:function(year){
			var _this = this;
			com.ajax({
				url:"/activity_year.json",
				data:{year:year},
				success:function(res){
					_this.genYearItems(res.data,year);
				}
			})
		},
		genYearItems:function(yearInfos,year){
			var html = "";
			var _this = this;
			$(yearInfos).each(function(idx){
				html += '<div class="item"> ';
				html += ' <div class="item-top" tg="' + this.id + '" par="' + year + '">';
				html += '	<div class="item-title"> ';
				html += this.reportTimeStr;
				html += '	</div> ';
				html += '	<div class="item-body"> ';
				html += '	  <div class="item-words"> ';
				html += '		<div class="item-word"> ';
				html += this.title;
				html += '		</div> ';
				html += '		<div class="item-subline"> ';
				html += this.roundupContext;
				html += '		</div> ';
				html += '	  </div> ';
				html += '	  <div class="item-img"> ';
				html += '		<img src="' + com.Config.domain + this.viewImg + '"> ';
				html += '	  </div> ';
				html += '	</div> ';
				html += ' </div>';
				html += ' <div class="item-main">';
				html += ' </div>';
				html += '</div> ';
			});
			$(".year-info[tg='" + year + "']").find(".year-items").html(html);
			$(".year-info[tg='" + year + "']").find(".item-top").on("click",function(){
				var id = $(this).attr("tg");
				var year = $(this).attr("par");
				var con = $(this).parent().find(".item-main");
				var top = this;
				if(!$(this).hasClass("expand")){
					$(".item-top").removeClass("expand");
					com.ajax({
						url:"/activity_detail.json",
						data:{id:id},
						success:function(res){
							con.html(com.utils().htmldecode(res.data.context));
							$(top).addClass("expand");
							$(window).scrollTop(_this.calcScroll(year,top));
						}
					})
				}else{
					$(this).removeClass("expand");					
				}
			});
		},
		calcScroll:function(curYear,ctrl){
			var totalHeight = 0;
			$(".year-info").each(function(idx){
				var tg = $(this).attr("tg");
				if(tg > curYear){
					totalHeight += (window.lib.flexible.rem2px("0.4") + $(this).height());
				}
			});
			var prevs = $(ctrl).parent().prevAll(".item");
			$(prevs).each(function(){
				totalHeight += $(this).height();
			})
			return totalHeight;
		}
	}
}